package dianmobile.eistarandroid.network.request;

import android.content.Context;

import com.android.volley.Response;

import org.xmlpull.v1.XmlPullParser;

import java.util.ArrayList;
import java.util.Map;

import dianmobile.eistarandroid.beans.BoardItemEntity;

public class SubscriptionListRequest extends ByhhAndroidRequest {

    public static void execute(Context context, RequestCallback callback) {
        executeSuper(context, RequestType.SUBSCRIPTION, null, callback);
    }

    public SubscriptionListRequest(int method, String url, Response.ErrorListener listener) {
        super(method, url, listener);
    }
    public SubscriptionListRequest(int method, String url, Map<String, String> params,
                        RequestCallback callback, Response.ErrorListener listener) {
        super(method, url, params, callback, listener);
    }

    @Override
    protected void parseXmlToMap(XmlPullParser parser, Map<String, Object> map) {
        ArrayList<BoardItemEntity> items = new ArrayList<>();
        BoardItemEntity item = null;

        final String UC_BOARD = "board";
        final String UC_MASTER = "master";
        final String UC_HAS_READ = "hasread";
        final String UC_BOARD_SEC = "boardsec";
        final String UC_BOARD_NAME = "boardname";
        final String UC_BOARD_TITLE = "boardtitle";
        final String UC_FILENUM = "filenum";

        try {
            int even = parser.getEventType();
            while (even != XmlPullParser.END_DOCUMENT) {
                switch (even) {
                    case XmlPullParser.START_DOCUMENT:
                        break;

                    case XmlPullParser.START_TAG:
                        String tagName = parser.getName();

                        if (UC_BOARD.equals(tagName)) {
                            item = new BoardItemEntity();
                            item.setIsBookmark(true);
                        }
                        else if (UC_HAS_READ.equals(tagName))
                            item.setHasRead(Boolean.valueOf(parser.nextText()));
                        else if (UC_BOARD_SEC.equals(tagName))
                            item.setBoardSec(parser.nextText());
                        else if (UC_BOARD_NAME.equals(tagName))
                            item.setBoardName(parser.nextText());
                        else if (UC_BOARD_TITLE.equals(tagName))
                            item.setBoardTitle(parser.nextText());
                        else if (UC_FILENUM.equals(tagName))
                            item.setFileNum(Integer.valueOf(parser.nextText()));

                        break;

                    case XmlPullParser.END_TAG:
                        String endTagName = parser.getName();

                        if (UC_BOARD.equals(endTagName)) {
                            items.add(item);
                            item = null;
                        }
                        break;
                    }

                even = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }

        map.put("result_data", items);
    }
}
